package com.canbot.u05.activity.presidemode;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.canbot.u05.R;
import com.canbot.u05.activity.presidemode.eventbus.AudioEvent;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;


/**
 * @author zmp
 * 主持人模式文字转语音界面
 */
public class HostModeTextToSpeechActivity extends BaseCompereActivity implements View.OnClickListener {

        private EditText editText;

        private ImageView btnBack, btnVoice;

        private Button btnSure;

        private String content;

        private int type;

        private ToastTools toastTools;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_host_mode_text_to_speech);
                toastTools = ToastTools.newInstance(this);
                Intent intent = getIntent();
                type = intent.getIntExtra("type", AudioEvent.AUDIO_TYPE_TTS);
                content = intent.getStringExtra("content");
                setViews();
        }

        private void setViews() {
                btnBack = (ImageView) findViewById(R.id.host_mode_imageView_back);
                btnVoice = (ImageView) findViewById(R.id.iv_host_mode_text_to_speech_voice_icon);
                btnSure = (Button) findViewById(R.id.iv_host_mode_text_to_speech_imageView_sure);
                editText = (EditText) findViewById(R.id.host_mode_editText_text_to_speech);
                btnBack.setOnClickListener(this);
                btnVoice.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                editText.addTextChangedListener(watcher);
                if (type == AudioEvent.AUDIO_TYPE_TTS && !TextUtils.isEmpty(content)) {
                        int len = content.length();
                        editText.setText(content);
                        editText.setSelection(len);
                }
        }

        private TextWatcher watcher = new TextWatcher() {

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                        if (s.length() > 2000) {
                                editText.setText(s.subSequence(0, 2000));
                                editText.setSelection(2000);
                                toastTools.showText(getString(R.string.input_maximum_words));
                        }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
        };

        @Override
        protected void onPause() {
                super.onPause();
        }

        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                        case R.id.host_mode_imageView_back:
                                finish();
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;
                        case R.id.iv_host_mode_text_to_speech_voice_icon:
                                //点击声音图标
                                String trim2 = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim2)) {
                                        toastTools.showText(getString(R.string.text_cannot_empty));
                                        return;
                                }
                                MsgSendUtils.sendStringMsg(MsgType.PLAY_TTS, trim2);
                                break;

                        case R.id.iv_host_mode_text_to_speech_imageView_sure:
                                //确定
                                String trim = editText.getText().toString().trim();
                                if (TextUtils.isEmpty(trim)) {
                                        toastTools.showText(getString(R.string.enter_text_content));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("type", AudioEvent.AUDIO_TYPE_TTS);
                                intent.putExtra("content", trim);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                }
        }

        public static void setDialogWindowAttr(Dialog dlg, Context ctx) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = (int) ctx.getResources().getDimension(R.dimen.x848);//宽高可设置具体大小
                lp.height = (int) ctx.getResources().getDimension(R.dimen.x499);
                dlg.getWindow().setAttributes(lp);
        }
}
